﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-4-21</createdate>
//<author>xier</author>
//<email>xier@tunynet.com</email>
//<log date="2008-4-21">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.ComponentModel.Design;
using ChinaCustoms.Qingdao.Workflow.Supports;
using System.Xml;
using System.Configuration;
using System.ComponentModel.Design;

namespace ChinaCustoms.Qingdao.Workflow.Designers
{
    public partial class DesignerForm : Form
    {
        private string defaultFormTitle = "海关工作流设计器";
        //WorkflowClient workflowClient = null;
        private int templateIDInRemote = 0;

        //zoom level combo box handling
        private bool zoomLevelDirty = false;

        public DesignerForm()
        {
            InitializeComponent();
            //workflowClient = new WorkflowClient("iisHostEndpointForWorkflow");
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.ShowDefaultWorkflow();
            this.Text = "新建工作流模板 - " + this.defaultFormTitle;
        }

        private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WorkflowView workflowView = this.workflowDesignerControl1.WorkflowView;
            if (workflowView != null)
            {
                WorkflowPageSetupDialog pageSetupDialog = new WorkflowPageSetupDialog(this.workflowDesignerControl1.WorkflowView as IServiceProvider);
                if (DialogResult.OK == pageSetupDialog.ShowDialog())
                {
                    workflowView.PerformLayout();
                }
            }
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WorkflowView workflowView = this.workflowDesignerControl1.WorkflowView;
            if (workflowView != null)
            {
                this.workflowDesignerControl1.SuspendLayout();
                workflowView.PrintPreviewMode = !workflowView.PrintPreviewMode;
                打印预览ToolStripMenuItem.Checked = workflowView.PrintPreviewMode;
                workflowView.ClientSize = this.workflowDesignerControl1.ClientSize;
                this.workflowDesignerControl1.ResumeLayout(true);
            }
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WorkflowView workflowView = this.workflowDesignerControl1.WorkflowView;
            if (null != workflowView)
            {
                //select printer
                PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
                printDialog.AllowPrintToFile = false;
                printDialog.Document = workflowView.PrintDocument;

                try
                {
                    printDialog.ShowDialog();
                }
                catch
                {
                    string errorString = "打印机错误,请选择其他的打印机!";
                    MessageBox.Show(this, errorString, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void 保存在本地ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.Save();
            this.Text = this.workflowDesignerControl1.Text + " - " + this.defaultFormTitle;
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.LoadExistingWorkflow();
            this.Text = this.workflowDesignerControl1.Text + " - " + this.defaultFormTitle;
        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void zoomInToolButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.ZoomIn);
            //this.zoomInToolButton.Checked = true;
            this.SetToolbarButtonCheck(zoomInToolButton);
        }

        private void zoomOut_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.ZoomOut);
            //this.zoomOut.Checked = true;
            this.SetToolbarButtonCheck(zoomOut);
        }

        private void panToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Pan);
            this.SetToolbarButtonCheck(panToolStripButton);
        }

        private void SetToolbarButtonCheck(ToolStripButton buttonChencked)
        { 
            int iconCount= this.toolStrip1.Items.Count;
            for (int i = 0; i < iconCount; i++)
            {
                if (this.toolStrip1.Items[i] is ToolStripButton)
                {
                    ((ToolStripButton)this.toolStrip1.Items[i]).Checked = false;
                }
            }

            if (buttonChencked != null)
            {
				buttonChencked.Checked = true;
            }
        }

        private void 保存到远程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //WorkflowTemplateInfo templateInfo = null;
            //if (this.templateIDInRemote == 0)
            //{
            //    templateInfo = new WorkflowTemplateInfo();
            //    FillTemplateInfo(templateInfo);
            //    this.templateIDInRemote = workflowClient.CreateWorkflowTemplate(templateInfo);
            //}
            //else
            //{
            //    templateInfo = workflowClient.GetWorkflowTemplate(templateIDInRemote);
            //    FillTemplateInfo(templateInfo);
            //    workflowClient.UpdateWorkflowTemplate(templateInfo);
            //}
        }

        /// <summary>
        /// 用当前的页面信息填充 工作流模板实体
        /// </summary>
        /// <param name="templateInfo"></param>
        private void FillTemplateInfo(WorkflowTemplateInfo templateInfo)
        {
            templateInfo.ApplicationID = Guid.NewGuid();
            templateInfo.ApplicationName = "测试工作流模板的应用";
            templateInfo.CreateDate = DateTime.Now;
            templateInfo.CreateUserID = Guid.NewGuid();

            templateInfo.FunctionID = Guid.NewGuid();
            templateInfo.FunctionName = "测试工作流模板的功能";
            templateInfo.IsDelete = false;
            templateInfo.IsPublic = true;
            templateInfo.WorkflowTemplateName = "测试工作流模板的名称";
            templateInfo.CreateUserName = "xier";


            string entityXML = this.workflowDesignerControl1.GetCurrentXoml();
            templateInfo.EntityXML = entityXML;
            templateInfo.CreateUserName = GetWorkflowProperty(entityXML, "DesignAuthor");
        }




        /// <summary>
        /// 获取工作流模板内的属性
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        private string GetWorkflowProperty(string xml, string propertyName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace("x", ConfigurationManager.AppSettings["msWFNameSpace"]);
            nsmgr.AddNamespace("ns0", ConfigurationManager.AppSettings["chinaCustomsSequentialWorkflowNameSpace"]);
            
            XmlNode xmlNode = xmlDoc.SelectSingleNode("ns0:ChinaCustomsSequentialWorkflow",nsmgr);
            XmlAttributeCollection attributeCollection = xmlNode.Attributes;
            XmlAttribute att;

            att = attributeCollection[propertyName];
            if (att != null)
            {
                return att.Value;
            }
            else
            {
                return string.Empty;
            }
        }


        private void ParseZoomLevelValue()
        {
            //parse the value
            string newZoom = zoomLevelToolStripButton.Text.Trim();
            if (newZoom.EndsWith("%"))
                newZoom = newZoom.Substring(0, newZoom.Length - 1);

            if (newZoom.Length > 0)
            {
                string errorMessage = null;

                try
                {
                    this.workflowDesignerControl1.WorkflowView.Zoom = Convert.ToInt32(newZoom);
                }
                catch (FormatException)
                {
                    errorMessage = "无效的缩放比例";
                }
                catch
                {
                    errorMessage = "无效的缩放范围";
                }

                if (errorMessage != null)
                {
                    MessageBox.Show(this, errorMessage);
                }
            }
        }

        private void zoomLevelToolStripButton_SelectedIndexChanged(object sender, EventArgs e)
        {
            ParseZoomLevelValue();
        }

        private void zoomLevelToolStripButton_Leave(object sender, EventArgs e)
        {
            if (this.zoomLevelDirty)
            {
                ParseZoomLevelValue();
                this.zoomLevelDirty = false;
            }
        }

        private void zoomLevelToolStripButton_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter && e.KeyCode != Keys.Escape)
            {
                zoomLevelDirty = true;
            }
        }

        private void zoomLevelToolStripButton_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ParseZoomLevelValue();
                zoomLevelDirty = false;
                this.workflowDesignerControl1.WorkflowView.Focus();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                //revert the changes back to the zoom level from the workflow
                zoomLevelToolStripButton.Text = this.workflowDesignerControl1.WorkflowView.Zoom + "%";
                zoomLevelDirty = false;
                this.workflowDesignerControl1.WorkflowView.Focus();
            }
            else
            {
                zoomLevelDirty = true;
            }
        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(StandardCommands.Cut);
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(StandardCommands.Copy);
        }

        private void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(StandardCommands.Paste);
        }

        private void expandToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Expand);
        }

        private void collapseToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Collapse);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Zoom400Mode);
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.WorkflowView.Zoom = 10;
        }

        private void 展示全部AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.ShowAll);
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Zoom300Mode);
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Zoom200Mode);
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Zoom100Mode);
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Zoom75Mode);
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.Zoom50Mode);
        }

        private void DesignerForm_Load(object sender, EventArgs e)
        {

        }

        private void zoomDefaultToolStripButton_Click(object sender, EventArgs e)
        {
            this.workflowDesignerControl1.InvokeStandardCommand(WorkflowMenuCommands.DefaultFilter);
            this.SetToolbarButtonCheck(zoomDefaultToolStripButton);
        }

        private void openToolStripButton_ButtonClick(object sender, EventArgs e)
        {
            //打开远程的模板
            MessageBox.Show("打开远程的模板");
            //TemplateChooser chooser = new TemplateChooser();
            //chooser.ShowDialog();
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox aboutBox = new AboutBox();
            aboutBox.ShowDialog();
        }
    }
}
